$(function () {
    $("#claim_datagrid").datagrid({
        url: '/claim/list',
        fit: true,
        fitColumns: true,
        columns: [[
            {field: 'xx', checkbox: true},
            {field: 'groupByType', title: '分组类型', width: 100},
            {field: 'totalAmount', title: '理赔总额', width: 100}
        ]],
        toolbar: "#tb",
        //新增
        border:false,
        cls:"table table-very table-basic",
        onLoadSuccess:function (data) {
            var rows = $("#claim_datagrid").datagrid("getRows");
            if(rows.length>0){
                $("#bar_dialog").dialog({
                    title:'理赔报表柱状图',
                    width: 750,
                    height: 500
                })
                var am = [];
                var type = [];
                for(var i = 0;i<rows.length;i++){
                    type[i] = rows[i].groupByType;
                    am[i] = rows[i].totalAmount;
                }
                console.log(am);
                var myChart = echarts.init(document.getElementById('main'));
                var option = {
                    title: {
                        text: '理赔报表',
                        subtext: $("#groupType").find("option:selected").text(),
                        x: 'center'
                    },
                    tooltip: {
                        trigger: 'axis'
                    },
                    legend: {
                        data: ['理赔总金额'],
                        x: 'left'
                    },
                    toolbox: {
                        show: true,
                        feature: {
                            mark: {show: true},
                            dataView: {show: true, readOnly: false},
                            magicType: {show: true, type: ['line', 'bar']},
                            restore: {show: true},
                            saveAsImage: {show: true}
                        }
                    },
                    calculable: true,
                    xAxis: [
                        {
                            type: 'category',
                            data: type
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value'
                        }
                    ],
                    series: [
                        {
                            name: '理赔总金额',
                            type: 'bar',
                            data: am,
                            markPoint: {
                                data: [
                                    {type: 'max', name: '最大值'},
                                    {type: 'min', name: '最小值'}
                                ]
                            },
                            markLine: {
                                data: [
                                    {type: 'average', name: '平均值'}
                                ]
                            }
                        }
                    ]
                };

                // 为echarts对象加载数据
                myChart.setOption(option);

            }else{
                $.messager.alert("温馨提示","没有可以显示的数据")
            }




        }

    })


    $("#groupType").change(function () {
        var groupType = $("#groupType").val();
        var organizationId = $("#organizationId").combobox("getValue")
        var productId = $("#productId").combobox("getValue")
        $("#claim_datagrid").datagrid("load", {
            groupType: groupType,
            organizationId: organizationId,
            productId: productId
        })
    })
})

function query() {
    var groupType = $("#groupType").val();
    var organizationId = $("#organizationId").combobox("getValue")
    var productId = $("#productId").combobox("getValue")
    $("#claim_datagrid").datagrid("load", {
        groupType: groupType,
        organizationId: organizationId,
        productId: productId,
    })
}



function chartBar() {
    var rows = $("#claim_datagrid").datagrid("getRows");
    if(rows.length>0){
            $("#bar_dialog").dialog({
                title:'销售报表柱状图',
                width: 750,
                height: 500
            })
            var am = [];
            var type = [];
            for(var i = 0;i<rows.length;i++){
                type[i] = rows[i].groupByType;
                am[i] = rows[i].totalAmount;
            }
        console.log(am);
        var myChart = echarts.init(document.getElementById('main'));
            var option = {
                title: {
                    text: '理赔报表',
                    subtext: $("#groupType").find("option:selected").text(),
                    x: 'center'
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: ['理赔总金额'],
                    x: 'left'
                },
                toolbox: {
                    show: true,
                    feature: {
                        mark: {show: true},
                        dataView: {show: true, readOnly: false},
                        magicType: {show: true, type: ['line', 'bar']},
                        restore: {show: true},
                        saveAsImage: {show: true}
                    }
                },
                calculable: true,
                xAxis: [
                    {
                        type: 'category',
                        data: type
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: [
                    {
                        name: '理赔总金额',
                        type: 'bar',
                        data: am,
                        markPoint: {
                            data: [
                                {type: 'max', name: '最大值'},
                                {type: 'min', name: '最小值'}
                            ]
                        },
                        markLine: {
                            data: [
                                {type: 'average', name: '平均值'}
                            ]
                        }
                    }
                ]
            };

            // 为echarts对象加载数据
            myChart.setOption(option);

        }else{
            $.messager.alert("温馨提示","没有可以显示的数据")
        }
}

function pieBar() {
    var rows = $("#claim_datagrid").datagrid("getRows");
    if(rows.length>0){
        $("#bar_dialog").dialog({
            title:'理赔报表饼状图',
            width: 750,
            height: 500
        })
        var am = [];
        var type = [];
        var pi ;
        var data =[];
        for(var i = 0;i<rows.length;i++){
            type[i] = rows[i].groupByType;
            am[i] = rows[i].totalAmount;
            pi = "{name:'"+type[i]+"',value:"+am[i]+"}";
            data[i]=pi;
        }
        data="["+data.toString()+"]"
        console.log(data);
        // 基于准备好的dom，初始化echarts图表
        var myChart = echarts.init(document.getElementById('main'));
        var option = {
            title : {
                text: '理赔报表',
                subtext:  $("#groupType").find("option:selected").text(),
                x:'center'
            },
            tooltip : {
                trigger: 'item',
                formatter: "{a} <br/>{b} : {c} ({d}%)"
            },
            legend: {
                orient : 'vertical',
                x : 'left',
                data:type
            },
            toolbox: {
                show : true,
                feature : {
                    mark : {show: true},
                    dataView : {show: true, readOnly: false},
                    magicType : {
                        show: true,
                        type: ['pie', 'funnel'],
                        option: {
                            funnel: {
                                x: '25%',
                                width: '50%',
                                funnelAlign: 'left',
                                max: Math.max.apply(null, am) //获取数组中的最大值
                            }
                        }
                    },
                    restore : {show: true},
                    saveAsImage : {show: true}
                }
            },
            calculable : true,
            series : [
                {
                    name:'理赔总金额',
                    type:'pie',
                    radius : '55%',
                    center: ['50%', '60%'],
                    data:eval('(' + data + ')')
                }
            ]
        };
        //[{name=admin, value=1677400.00}, {name=李四明, value=288000.00}]
        // 为echarts对象加载数据
        myChart.setOption(option);

    }else{
        $.messager.alert("温馨提示","没有可以显示的数据")
    }
}